import os
import bottle
import uuid
import urllib
import subprocess
from bottle import route, run, template,request
@route('/alisa')
def index():
    params = bottle.request.params
    filename=params.get("filename", None)
    print(filename)
   # command = "python /opt/caffe/build/open_nsfw/classify_nsfw.py --model_def /opt/caffe/build/open_nsfw/nsfw_model/deploy.prototxt --pretrained_model /opt/caffe/build/open_nsfw/nsfw_model/resnet_50_1by2_nsfw.caffemodel /home/wangqi/open_nsfw-master/pic/"+filename
    command = "docker run --volume=$(pwd):/workspace caffe:cpu python ./classify_nsfw.py --model_def nsfw_model/deploy.prototxt --pretrained_model nsfw_model/resnet_50_1by2_nsfw.caffemodel 1.jpg"


    print(os.popen(command))
    # output = os.popen(command)
    

    return template('<b>{{text}}</b>', text=subprocess.getoutput(command))
    #return template('<b>{{text}}</b>', text="srgrsezg")


upload_path='static/'

@route('/upload')
def upload():
    return template('upload') 

@route('/upload',method='POST')
def do_upload():
    uploadfile=request.files.get('data')
    uploadfile.save(upload_path,overwrite=True)
    command = "python /opt/caffe/build/open_nsfw/classify_nsfw.py --model_def /opt/caffe/build/open_nsfw/nsfw_model/deploy.prototxt --pretrained_model /opt/caffe/build/open_nsfw/nsfw_model/resnet_50_1by2_nsfw.caffemodel "+upload_path+uploadfile.filename
    #command = "docker run --volume=$(pwd):/workspace caffe:cpu python ./classify_nsfw.py --model_def nsfw_model/deploy.prototxt --pretrained_model nsfw_model/resnet_50_1by2_nsfw.caffemodel "+upload_path+uploadfile.filename
    content=os.popen(command).read()
    os.remove(upload_path+uploadfile.filename)
    return template('{{text}}', text= content[14:18])


@route('/uploadUrl')
def do_upload_url():
   
    picurl = request.params.get("url", None)
   
    picName=upload_path+str(uuid.uuid1())+'.jpg';
    
   
    urllib.urlretrieve(picurl,picName)
   
    command = "python /opt/caffe/build/open_nsfw/classify_nsfw.py --model_def /opt/caffe/build/open_nsfw/nsfw_model/deploy.prototxt --pretrained_model /opt/caffe/build/open_nsfw/nsfw_model/resnet_50_1by2_nsfw.caffemodel "+picName
    content=os.popen(command).read()

    os.remove(picName)
    return template('{{text}}', text= content[14:18])

run(host='0.0.0.0', port=8888)
